<?php
$isReloadable = true;
require_once("../../../pers/inc.config.php");
require_once('../../_inc/inc.session.php');
require_once('../../_inc/funct.html.php');
require_once('../../_inc/funct.getFiles.php');

$file = reqVar('cod');
$aFileX = getFiles($file);	
//dbg_($aFileX);
$aDefCampi = getDefCampi($file);
if ($aDefCampi===false) {
	backOnErr("fileView - manca cod");
	exit();
}

$cmdL = "";

$cmdR = "";
if (true) {
	$cmdR .= "&nbsp;&nbsp;&nbsp;";
	$cmdR .= "<a class=\"cmd1\" href=\"fileCamIns.php?cod=".reqVar('cod')."&$sXSNQS\">nuovo campo</a>"; 
	$cmdR .= "&nbsp;&nbsp;&nbsp;";
	$cmdR .= "<a class=\"cmd1\" href=\"javascript:alert('work in progress');\">nuovo indice</a>"; 
	//$cmdR .= "<a class=\"cmd1\" href=\"fileIndIns.php?cod=".reqVar('cod')."&$sXSNQS\">nuovo indice</a>"; 
}
$cmdR .= cmdTorna(); 

testaHtml('Gestione file', 'File',$msgPag, 'gest');
echo getHtmlForm();
echo getHtmlHidden('azione','reload');
echo getHtmlHidden('xsn',XSN);
echo getHtmlHidden('cod');
?>
<table class="cmd1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left">
         <?=$cmdL?>
      </td>
      <td class="cmd1" align="right">
         <?=$cmdR?>
      </td>
   </tr>
</table>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="lab1" width='15%' align='left'>File</td>
      <td class="txt1" width='25%' align='left'>
			<font style='font-family:lucida console,courier;'><?=$file?></font>
		</td>
      <td class="txt1" width='60%' align='left'>
			<?=$aFileX['descr']?>
		</td>
   </tr>
   <tr>
      <td class="lab1" valign='top'>Chiave primaria</td>
      <td class="txt1" colspan='2' align='left'>
			<?=$aFileX['primary']?>
		</td>
   </tr>
</table>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="lab2" align='center' colspan="5">Campi</td>
   </tr>
   <tr>
      <td class="lab1" align='center' width="15%">Campo</td>
      <td class="lab1" align='center' width="15%">Tipo</td>
      <td class="lab1" align='center' width="15%">Default</td>
      <td class="lab1" align='center' width="15%">&nbsp;</td>
      <td class="lab1" align='center' width="40%">Nota</td>
   </tr>
<?php 
foreach($aDefCampi as $campo => $aDC) {
?>
   <tr>
      <td class="txt1" align='left'  >
      	<?
      	$hrefX = "fileCamMod.php?cod=".$file."&campo=$campo&$sXSNQS";
      	if (isset($aFileX['campiFix'][$campo])) {
      		echo "<b>$campo</b>";
      	} else {
      		echo "<a href='$hrefX' class='txt1'>$campo</a>";
      	}
      	?>
      </td>
      <td class="txt1" align='left'  ><?=$aDC[0]?></td>
      <td class="txt1" align='left'  ><?=$aDC[1]?></td>
      <td class="txt1" align='center'><?=$aDC[2]?></td>
      <td class="txt1" align='left'  ><?=$aDC[3]?></td>
   </tr>
<?php 
}
?>
</table>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="lab2" align='center' colspan="2">Indici</td>
   </tr>
   <tr>
      <td class="lab1" align='center' width="20%">Indice</td>
      <td class="lab1" align='center' width="80%">Campi</td>
   </tr>
<?php 
foreach($aFileX['index'] as $indice => $sCampiInd) {
?>
   <tr>
      <td class="txt1" align='left'  ><?=$indice?></td>
      <td class="txt1" align='left'  ><?=$sCampiInd?></td>
   </tr>
<?php 
}
?>
</table>
<table class="cmd1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left">
         <?=$cmdL?>
      </td>
      <td class="cmd1" align="right">
         <?=$cmdR?>
      </td>
   </tr>
</table>
<?
echo "</file>\n";
codaHtml();
exit();

function getFileDef($file) {
	global $objDB;
	$aFiles = getFiles();	
	if (!isset($aFiles[$file])) {
		backOnErr("fileView - manca cod");
		exit();
	}
	$aFiledDef = array('nome'=>$file,'descr'=>ovfVx($aFiles,$file,'descr')
		,'nota'=>ovfVx($aFiles,$file,'nota'),'campi'=>array(),'indici'=>array());
	$aCampiDef = getDefCampi($file);
	
	/*/carica array campi
	$bAltered = false;
	foreach($aFiles[$file]['campiFix'] as $campo => $aDefCampo ) {
		//verifica esistenza campo
		if (!isset($aCampiDef[$campo])) {
			$sSql = "ALTER TABLE $file ADD COLUMN $campo ".getStrDefCampo($aDefCampo);
			$objDB->query($sSql,"showCol",false);
			if ($objDB->errno("showCol")!=0) {
				$aFiledDef['msgpag'] = $objDB->error("showCol");
				break;
			}
			$bAltered = true;
			continue;
		}
		//verifica modifiche campo
		$oldCampoDef = getStrDefCampo($aCampiDef[$campo]);
		$newCampoDef = getStrDefCampo($aDefCampo);
		if ($oldCampoDef!=$newCampoDef) {
			$sSql = "ALTER TABLE $file MODIFY COLUMN $campo ".$newCampoDef;
			$objDB->query($sSql,"showCol",false);
			if ($objDB->errno("showCol")!=0) {
				$aFiledDef['msgpag'] = $objDB->error("showCol");
				break;
			}
			$bAltered = true;
			continue;
		}
	}
	// se modificato ricarica def. campi
	if ($bAltered) {
		$aCampiDef = getDefCampi($file);
		if (ovfVx($aCampiDef,'*errno*')!=0) {
			return ovfVx($aCampiDef,'*error*');
		}
	}
	
	$aFiledDef['campi'] = $aCampiDef;
	$sSql = "SHOW INDEX IN ".$file;
	$objDB->query($sSql,"showCol",false);
	while($acX = $objDB->fetch("showCol")) {
		$aFiledDef['indici'][$acX['Key_name']][$acX['Seq_in_index']] = $acX['Column_name'];
	}
	foreach($aFiles[$file]['index'] as $index => $sKeys) {
		if (!isset($aFiledDef['indici'][$index])) {
			$sSql = "ALTER TABLE $file ADD KEY $index ($sKeys)";
			$objDB->query($sSql,"showCol",false);
			if ($objDB->errno("showCol")!=0) {
				$aFiledDef['msgpag'] = $objDB->error("showCol");
			}
			$aFiledDef['indici'][$index] = explode(",",$sKeys);
			continue;
		}
	}
	//*/////////////////////////
	return $aFiledDef;
}

function getStrDefCampo($aDefCampo) {
	$sDef = $aDefCampo[0]." NOT NULL";
	if ($aDefCampo[1]!='') {
		$sDef .= " DEFAULT '".$aDefCampo[1]."'";
	}
	if ($aDefCampo[2]!='') {
		$sDef .= " ".$aDefCampo[2];
	}
	if ($aDefCampo[3]!='') {
		$sDef .= " COMMENT '".addslashes($aDefCampo[3])."'";
	}
	return $sDef;
}

